﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Roslyn.Compilers.CSharp;

namespace RoslynHelper
{
    public static class MethodDeclaration
    {
        public static MethodDeclarationSyntax SetGenericConstraints(this MethodDeclarationSyntax node, IEnumerable<TypeParameterConstraintClauseSyntax> constraints)
        {
            return Syntax.MethodDeclaration(
                node.Attributes, 
                node.Modifiers, 
                node.ReturnType, 
                node.ExplicitInterfaceSpecifierOpt,
                node.Identifier,
                node.TypeParameterListOpt,
                node.ParameterList,
                Syntax.List<TypeParameterConstraintClauseSyntax>(constraints.Where(cc => cc != null)),
                node.BodyOpt,
                node.SemicolonTokenOpt);
        }

        public static MethodDeclarationSyntax AddParameters(this MethodDeclarationSyntax node, params ParameterSyntax[] paramaters)
        {
            return AddParameters(node, (IEnumerable<ParameterSyntax>)paramaters);
        }

        public static MethodDeclarationSyntax AddParameters(this MethodDeclarationSyntax node, IEnumerable<ParameterSyntax> paramaters)
        {
            return Syntax.MethodDeclaration(
                node.Attributes,
                node.Modifiers,
                node.ReturnType,
                node.ExplicitInterfaceSpecifierOpt,
                node.Identifier,
                node.TypeParameterListOpt,
                Syntax.ParameterList(
                    node.ParameterList.OpenParenToken,
                    SeparatedList.Create<ParameterSyntax>(node.ParameterList.Parameters.Union(paramaters)),
                    node.ParameterList.CloseParenToken),
                node.ConstraintClauses,
                node.BodyOpt,
                node.SemicolonTokenOpt);
        }
    }
}
